package com.example.demo.order.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeOrderinfoSyncModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradeOrderinfoSyncRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeOrderinfoSyncResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.bill.entity.PayAutoDkBill;
import com.example.demo.bill.service.IPayAutoDkBillService;
import com.example.demo.order.entity.Stages;
import com.example.demo.order.service.IStagesService;
import com.example.demo.order.service.IZhiMa;
import com.google.gson.Gson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

import lombok.extern.slf4j.Slf4j;


/**
 * Created at 2021/3/5 12:38 下午
 *
 * @author Alex
 * description
 */
@Slf4j
@Service
public class ZhiMaImpl implements IZhiMa {
    @Autowired
    private IStagesService stagesService;

    @Autowired
    private DefaultAlipayClient alipayClient;

    @Autowired
    private IPayAutoDkBillService iPayAutoDkBillService;


    @Override
    public boolean queryStageStatus(String stageId) {
        Stages stage = stagesService.getById(stageId);
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(stage.getOutOrderId());
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizModel(model);
        try {
            AlipayTradeQueryResponse response = alipayClient.execute(request);
            if ("10000".equals(response.getCode()) && "TRADE_SUCCESS".equals(response.getTradeStatus())) {
                uploadPaySuccess(stageId, "");

            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return false;
    }

    @Override
    public boolean uploadPaySuccess(String s, String status) {
        LambdaQueryWrapper<PayAutoDkBill> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PayAutoDkBill::getDkStatus, 2);
        queryWrapper.eq(PayAutoDkBill::getIsReport, 0);
        queryWrapper.eq(PayAutoDkBill::getIsDelete, 0);
        List<PayAutoDkBill> results = iPayAutoDkBillService.list(queryWrapper);
        results.stream().forEach(result -> {
            if (null != result && !result.getOutTradeNo().contains(",")) {
                AlipayTradeOrderinfoSyncRequest request = new AlipayTradeOrderinfoSyncRequest();
                AlipayTradeOrderinfoSyncModel model = new AlipayTradeOrderinfoSyncModel();
                model.setBizType("CREDIT_AUTH");
                model.setTradeNo(result.getTradeNo());
                model.setOutRequestNo(result.getOutTradeNo());
                model.setOrderBizInfo("{\"orderInfo\":\"{\\\"status\\\":\\\"COMPLETE\\\"}\"}");
                request.setBizModel(model);
                AlipayTradeOrderinfoSyncResponse response = null;
                try {
                    response = alipayClient.execute(request);
                    if ("10000".equals(response.getCode())) {
                        //上报成功，修改标识位，下次不再上报
                        UpdateWrapper<PayAutoDkBill> updateWrapper = new UpdateWrapper<>();
                        updateWrapper.eq("out_trade_no", result.getOutTradeNo()).set("is_report", 1);
                        iPayAutoDkBillService.update(updateWrapper);
                        log.info("上报芝麻信用成功: {}" + response.getBody());
                    } else {
                        log.error("上报芝麻信用失败: {}" + response.getBody());
                    }
                } catch (AlipayApiException e) {
                    e.printStackTrace();
                    log.error("上报芝麻信用出错: {}" + e.getMessage());
                }
            }
        });

        log.info("上报芝麻信用任务完毕");
        return false;
    }
}
